/*访问后台的代码*/
layui.define([], function(exports) {
    var down =function(){
        var canvas = $('#qrcode').find("canvas").get(0);
        var url = canvas.toDataURL('image/jpeg');
        $("#download").attr('href', url).get(0).click();
        return false;
    }
    var api = {
        updateUser : function(form, callback) {
            Lib.submitForm("/admin/user/update.json", form, {}, callback)
        },
        addUser : function(form, callback) {
            Lib.submitForm("/admin/user/add.json", form, {}, callback)
        },
        del : function(ids, callback) {
            Common.post("/admin/user/delete.json", {
                "ids" : ids
            }, function() {
                callback();
            })
        },
        downloadqr: function(userId,name, callback) {
            Common.post("/cms/user/downloadqr.json", {
                "id" : userId
            }, function(rsp) {
                $('#download').attr("download",name+"的绑定码.jpg");

                var div=document.getElementById("qrcode");
                div.innerHTML="";
                new QRCode(div, {
                    width: 200,
                    height: 200,
                    text: rsp,
                    img_src: "../../logo.jpg",
                    img_width: 80,
                    // use_canvas: false // (是否使用canvas绘制，默认true)
                    // 使用img_src，需要保证图片是支持跨域访问的，不然我也无能为力
                });
                setTimeout(function(){
                    down();
                },1000)
            })

        },

        changePassword : function(form, callback) {
            Lib.submitForm("/admin/user/changePassword.json", form, {},
                    callback)
        },
        addUserRole : function(form, callback) {
            Lib.submitForm("/admin/user/role/add.json", form, {}, callback)
        },
        delUserRole : function(ids, callback) {
            Common.post("/admin/user/role/delete.json", {
                "ids" : ids
            }, function() {
                callback();
            })
        },
        exportUsers:function(form,callback){
            var formPara = form.serializeJson();
            Common.post("/admin/user/excel/export.json", formPara, function(fileId) {
                callback(fileId);
            })
        }

    };

    exports('userApi', api);

});